#include <iostream>

#include "Image.H"
#include "Util.H"
#include "IO.H"


using namespace std;
using namespace pip;

int main(int argc, char *argv[])
{
  const string program = argv[0]; 
  vector<string> args(argv, argv + argc);
  if (argc < 2) {  //usage without flags (as given in the example on the exercise's page
    error(program, "Usage: PROGRAM REFERENCE_IMAGE PREDICTED_IMAGE");
  }

  Image<unsigned char> ref;
  Image<unsigned char> res;
  if(    !importFile(ref,args[1]) 
	  || !importFile(res,args[2]))
	  error(program,"The two given files can not be imported.");

  if ( ref.dim(1) > 1 ||
	   res.dim(1) > 1 )
	  error(program,"The two given files should be one dimensional.");

  vector<vector<long> > conMat; // assuming 10 digits - has to be changed for other applications
  vector<long> count;
  int length(res.dim(0)), nr_labels(10),hits(0),hits_all(0);

  for(int fill=0; fill < nr_labels; ++fill)
	  count.push_back(0);
  for(int fill=0; fill < nr_labels; ++fill)
	  conMat.push_back(count);

  for(unsigned int len = 0 ; len < length ; ++len){
	conMat[ref(len,0)][res(len,0)]++;
	count[ref(len,0)]++;
  }

  cout << "Actual label is leftmost column and predicted labels are the top row.\n # # # # # # # \n ";
  for(unsigned int top = 0 ; top < nr_labels ; ++top){
	cout << "\t" << top;
  }
  cout<< "\t acc." << endl << endl;
  for(unsigned int row = 0 ; row < nr_labels ; ++row){
	cout << row << " | ";
	for(unsigned int line = 0 ; line < nr_labels ; ++line){
		cout << "\t" << conMat[row][line];
	}
	hits=conMat[row][row];
	hits_all+=hits;

	cout << "\t" << float(hits) / float(count[row]) << endl;
  }

  cout << endl << "Overall: " << float(hits_all)/float(length) << endl;

  //system("pause");
  return 0;
}